<?php

namespace app\admin\controller;

use app\admin\controller\Utils\FileImport;
use app\app\model\ProjectModel;
use app\BaseController;
use PhpOffice\PhpSpreadsheet\Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Filesystem;

class JobTicket extends BaseController
{
    /**
     * 导入作业票
     * @return void
     * @throws Exception
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function import()
    {
        // 获取表单上传文件
        if (empty(request()->file('file'))) {
            $this->error("请上传文件！");
        }

        try {
            $file = request()->file();
        } catch (\Exception $e) {
            $this->error("文件上传失败");
        }

        try {
            // 验证文件大小，名称等是否正确
            validate([
                'file' => 'filesize:52428800|fileExt:xls,xlsx'
            ])
                ->check($file);
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }

        $file_path = root_path('runtime') . 'storage/';
        $file_name = Filesystem::putFile(date('Ymd') . '/' . 'job_ticket', $file['file']);

        $data = (new FileImport())->ExcelImport($file_path . $file_name, 1);

        $insert_array = [];
        $error_array = [];
        $i = 0;

        foreach ($data as $k => $v) {
            $i++;

            if ($k == 0) continue;
            if ($k == 1) continue;
            if (empty($v[3])) continue;

            $project = ProjectModel::where('project_name', $v[3])
                ->where('is_parent', 1)
                ->find();
            if (empty($project)) {
                $error_array[] = '第' . $i .  '行项目名称异常';
                continue;
            }

            $project_id = $project->id;

            $insert_array[] = [
                'company' => $v[1] ?: null,
                'project_unit' => $v[2] ?: null,
                'project_name' => $v[3],
                'project_id' => $project_id,
                'section_name' => $v[4] ?: null,
                'ticket_name' => $v[5] ?: null,
                'ticket_no' => $v[6] ?: null,
                'construction_team' => $v[7] ?: null,
                'risk_level_preliminary_exploration' => $v[8] ?: null,
                'risk_level_after_retest' => $v[9] ?: null,
                'operation_type' => $v[10] ?: null,
                'planned_start_time' => $v[11] ?: null,
                'planned_end_time' => $v[12] ?: null,
                'actual_start_time' => $v[13] ?: null,
                'actual_end_time' => $v[14] ?: null,
                'implementation_scheme_name' => $v[15] ?: null,
                'construction_personnel_num' => $v[16] ?: null,
                'technical_points_scheme' => $v[17] ?: null,
                'detail_work_charge_person' => $v[18] ?: null,
                'detail_safety_guardian' => $v[19] ?: null,
                'detail_construction_technicians' => $v[20] ?: null,
                'detail_other_construction_personnel' => $v[21] ?: null,
                'detail_temporary_assistance' => $v[22] ?: null,
                'major_risks' => $v[23] ?: null,
                'changes' => $v[24] ?: null,
                'control_measures' => $v[25] ?: null,
                'work_charge_person' => $v[26] ?: null,
                'reviewer' => $v[27] ?: null,
                'safety_guardian_person' => $v[28] ?: null,
                'issued_person' => $v[29] ?: null,
                'supervision_personnel' => $v[30] ?: null,
                'process_status' => $v[33] ?: null,
                'remark' => $v[34] ?: null,
                'shift_meetings_num' => $v[35] ?: null,
                'qr_code_no' => $v[36] ?: null,
                'qr_code_name' => $v[37] ?: null,
                'is_create_shift_meeting' => $v[38] ?: null,
                'ticket_location' => $v[39] ?: null,
                'shift_meeting_location' => $v[40] ?: null,
                'shift_meeting_create_time' => $v[41] ?: null,
                'unit_name' => $v[42] ?: null,
            ];
        }

        (!empty($error_array)) && $this->error('上传异常：' . implode('，', $error_array));

        try {
            Db::table('t_ticket_stat')->insertAll($insert_array);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('上传成功');
    }
}